﻿namespace Comet.View.Wpf.Converters
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using Comet.Infrastructure;
    using Comet.ViewModel;
    using System.Resources;

    [ValueConversion(typeof(DataMemberViewModel), typeof(BitmapSource))]
    public class DataMemberToIconConverter : IValueConverter
    {
        static Dictionary<DataMemberType, Dictionary<DataMemberVisibility, BitmapSource>> icons = new Dictionary<DataMemberType, Dictionary<DataMemberVisibility, BitmapSource>>();

        static DataMemberToIconConverter()
        {
            //icons[DataMemberType.Field] = new Dictionary<DataMemberVisibility, BitmapSource>();
            //icons[DataMemberType.Field][DataMemberVisibility.PublicAccess] = LoadImage("pack://application:,,,/Field_Public.png");
            //icons[DataMemberType.Field][DataMemberVisibility.PrivateAccess] = LoadImage("pack://application:,,,/Field_Public.png");
            //icons[DataMemberType.Field][DataMemberVisibility.ProtectedAccess] = LoadImage("pack://application:,,,/Field_Public.png");
            //icons[DataMemberType.Field][DataMemberVisibility.InternalAccess] = LoadImage("pack://application:,,,/Field_Public.png");

            //icons[DataMemberType.Property] = new Dictionary<DataMemberVisibility, BitmapSource>();
            //icons[DataMemberType.Property][DataMemberVisibility.PublicAccess] = LoadImage("pack://application:,,,/Property_Public.png");
            //icons[DataMemberType.Property][DataMemberVisibility.PrivateAccess] = LoadImage("pack://application:,,,/Property_Private.png");
            //icons[DataMemberType.Property][DataMemberVisibility.ProtectedAccess] = LoadImage("pack://application:,,,/Property_Protected.png");
            //icons[DataMemberType.Property][DataMemberVisibility.InternalAccess] = LoadImage("pack://application:,,,/Property_Internal.png");         

            global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Comet.View.Wpf.HostWpfControlForm", typeof(HostWpfControlForm).Assembly);

            icons[DataMemberType.Field] = new Dictionary<DataMemberVisibility, BitmapSource>();
            icons[DataMemberType.Field][DataMemberVisibility.PublicAccess] = ToBitmapSource(GetImage(temp, "Field_Public"));
            icons[DataMemberType.Field][DataMemberVisibility.PrivateAccess] = ToBitmapSource(GetImage(temp, "Field_Private"));
            icons[DataMemberType.Field][DataMemberVisibility.ProtectedAccess] = ToBitmapSource(GetImage(temp, "Field_Protected"));
            icons[DataMemberType.Field][DataMemberVisibility.InternalAccess] = ToBitmapSource(GetImage(temp, "Field_Internal"));

            icons[DataMemberType.Property] = new Dictionary<DataMemberVisibility, BitmapSource>();
            icons[DataMemberType.Property][DataMemberVisibility.PublicAccess] = ToBitmapSource(GetImage(temp, "Property_Public"));
            icons[DataMemberType.Property][DataMemberVisibility.PrivateAccess] = ToBitmapSource(GetImage(temp, "Property_Private"));
            icons[DataMemberType.Property][DataMemberVisibility.ProtectedAccess] = ToBitmapSource(GetImage(temp, "Property_Protected"));
            icons[DataMemberType.Property][DataMemberVisibility.InternalAccess] = ToBitmapSource(GetImage(temp, "Property_Internal"));  

            //icons[DataMemberType.Field] = new Dictionary<DataMemberVisibility, BitmapSource>();
            //icons[DataMemberType.Field][DataMemberVisibility.PublicAccess] = ToBitmapSource(HostWpfControlForm.Field_Public);
            //icons[DataMemberType.Field][DataMemberVisibility.PrivateAccess] = ToBitmapSource(HostWpfControlForm.Field_Private);
            //icons[DataMemberType.Field][DataMemberVisibility.ProtectedAccess] = ToBitmapSource(HostWpfControlForm.Field_Protected);
            //icons[DataMemberType.Field][DataMemberVisibility.InternalAccess] = ToBitmapSource(HostWpfControlForm.Field_Internal);

            //icons[DataMemberType.Property] = new Dictionary<DataMemberVisibility, BitmapSource>();
            //icons[DataMemberType.Property][DataMemberVisibility.PublicAccess] = ToBitmapSource(HostWpfControlForm.Property_Public);
            //icons[DataMemberType.Property][DataMemberVisibility.PrivateAccess] = ToBitmapSource(HostWpfControlForm.Property_Private);
            //icons[DataMemberType.Property][DataMemberVisibility.ProtectedAccess] = ToBitmapSource(HostWpfControlForm.Property_Protected);
            //icons[DataMemberType.Property][DataMemberVisibility.InternalAccess] = ToBitmapSource(HostWpfControlForm.Property_Internal);  
        }

        public static System.Drawing.Bitmap GetImage(ResourceManager r, string key)
        {
            System.Drawing.Bitmap b = r.GetObject(key) as System.Drawing.Bitmap;
            return b;
        }

        private static BitmapSource LoadImage(string path)
        {            
            using (Stream imageStreamSource = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                
                PngBitmapDecoder decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                return decoder.Frames[0];
            }
        }

        public static BitmapSource ToBitmapSource(System.Drawing.Bitmap bitmap)
        {
            using (System.IO.MemoryStream stream = new MemoryStream())
            {
                bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
                stream.Position = 0;

                var result = BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
                result.Freeze();
                return result;
            }
        }

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            DataMemberViewModel viewModel = value as DataMemberViewModel;

            if (viewModel != null)
            {
                return icons[viewModel.DataMemberType][viewModel.DataMemberVisibility]; 
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
